package base

import "jvm/rtda"

type Instruction interface {
	FetchOperands(reader *BytecodeReader) //获取操作数
	Execute(jFrame *rtda.JFrame)          //执行字节码指令
}

// NoOperandsInstruction 无操作数指令
type NoOperandsInstruction struct {
}

func (noi *NoOperandsInstruction) FetchOperands(reader *BytecodeReader) {
	// nothing to do
}

// BranchInstruction 跳转指令，存放偏移量
type BranchInstruction struct {
	Offset int
}

func (bi *BranchInstruction) FetchOperands(reader *BytecodeReader) {
	bi.Offset = int(reader.ReadInt16())
}

func Branch(jFrame *rtda.JFrame, offset int) {
	pc := jFrame.JThread().PC()
	nextPC := pc + offset
	jFrame.SetNextPC(nextPC)
}

// Index8Instruction iload、istore获取这样的索引
type Index8Instruction struct {
	Index uint
}

func (ii *Index8Instruction) FetchOperands(reader *BytecodeReader) {
	ii.Index = uint(reader.ReadUint8())
}

// Index16Instruction 访问运行时常量池的索引
type Index16Instruction struct {
	Index uint
}

func (ii *Index16Instruction) FetchOperands(reader *BytecodeReader) {
	ii.Index = uint(reader.ReadUint16())
}
